//
//  IGProductVC.m
//  jingdongfang
//
//  Created by BOE on 2017/9/22.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGProductVC.h"
#import "IGArticleCell.h"
#import "IGMainSearchApi.h"
#import "IGNetReachable.h"
#import "FindLayout.h"
#import "ArtGalleryCell_third.h"
#import "IGDatabaseApi.h"
#import "IGDatabaseDetailApi.h"
#import "XiangQingViewController.h"
#import "IGEmptyView.h"

@interface IGProductVC ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, DZNEmptyDataSetDelegate, DZNEmptyDataSetSource>

@property (nonatomic, strong) UICollectionView *collect;
@property (nonatomic, strong) NSMutableArray *productMuArr;
@property (nonatomic, strong) IGMainSearchApi *searchApi;
@property (nonatomic, assign) NSInteger page;

@end

@implementation IGProductVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor mainBackgroundColor];
    
    self.page = 0;
    self.searchApi = [IGMainSearchApi new];
    self.searchApi.type = IGSearchTypeProduct;
    
    [self.view addSubview:self.collect];
    
    [self.collect mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    __weak typeof(self) weakSelf = self;
    [self.collect ig_setHeaderWithRefreshingBlock:^{
        weakSelf.page = 0;
        [weakSelf requestData];
    }];
}

- (UICollectionView *)collect {
    if (!_collect) {
        FindLayout *layout = [[FindLayout alloc] init];
        _collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT) collectionViewLayout:layout];
        _collect.backgroundColor = [UIColor mainBackgroundColor];
        _collect.alwaysBounceVertical = YES;
        _collect.delegate = self;
        _collect.dataSource = self;
        _collect.emptyDataSetSource = self;
        _collect.emptyDataSetDelegate = self;
        [_collect registerClass:[ArtGalleryCell_third class] forCellWithReuseIdentifier:@"ArtGalleryCell_third"];
    }
    return _collect;
}

- (NSMutableArray *)productMuArr {
    if (!_productMuArr) {
        _productMuArr = [NSMutableArray array];
    }
    return _productMuArr;
}

- (void)setSearchKey:(NSString *)searchKey {
    if (_searchKey == searchKey || !searchKey || [searchKey isEqualToString:@""]) {
        return;
    }
    _searchKey = searchKey;
    self.page = 0;
    [self requestData];
}

- (void)requestData {
    self.searchApi.page = self.page;
    self.searchApi.searchKey = self.searchKey;
    __weak typeof(self) weakSelf = self;
    [self.searchApi startWithDecodeClass:[IGMainSearchResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [weakSelf.collect ig_endHeaderRefreshing];
        [weakSelf.collect ig_endFooterRefreshing];
        if (responseObject) {
            IGMainSearchResp *resp = responseObject;
            IGMainSearchModel *model = resp.data;
            if (model.lstProduct && model.lstProduct.count > 0) {
                [weakSelf setupFooter];
            } else {
                [weakSelf.collect ig_endFooterRefreshingWithNoMoreData];
            }
            
            if (weakSelf.page == 0) {
                weakSelf.productMuArr = [NSMutableArray arrayWithArray:model.lstProduct];
            } else {
                [weakSelf.productMuArr addObjectsFromArray:model.lstProduct];
            }
            [weakSelf.collect reloadData];
        } else {
            //空白页处理逻辑写在这里
        }
    }];
}

- (void)setupFooter
{
    __weak typeof(self) weakSelf = self;
    [self.collect ig_setFooterWithRefreshingBlock:^{
        weakSelf.page++;
        
        [weakSelf requestData];
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.productMuArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ArtGalleryCell_third *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ArtGalleryCell_third" forIndexPath:indexPath];
    IGArtWorkModel *base = _productMuArr[indexPath.item];
    cell.imgUrl = base.image;
    cell.titleL = base.title;
    [cell setNeedsLayout];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    IGArtWorkModel *base = _productMuArr[indexPath.item];
    if (self.search4PubBlock) {
        self.search4PubBlock(base);
        return;
    }
    XiangQingViewController *vc = [[XiangQingViewController alloc] init];
    vc.p_id = base.aId;
    vc.isRoot = YES;
    [UIViewController ig_pushViewController:vc animated:YES];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    IGArtWorkModel *base = _productMuArr[indexPath.item];
    if ([base.plates integerValue]==1)
    {
        //横板
        return CGSizeMake((KSCREENWIDTH-16)/2, ((KSCREENWIDTH-16)/2*1080/1920));
    }
    else if ([base.plates integerValue]==2)
    {
        //坚板
        return CGSizeMake((KSCREENWIDTH-16)/2, ((KSCREENWIDTH-16)/2*1920/1080));
    }
    return CGSizeMake(0, 0);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);
}

#pragma mark -Empty
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    IGEmptyView *emptyView = [[IGEmptyView alloc] init];
//    emptyView.title = @"暂无数据~";
    emptyView.title = NSLocalizedString(@"No Data", nil);
    emptyView.requestApi = self.searchApi;
    return emptyView;
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    return YES;
}

- (void)emptyDataSetDidTapView:(UIScrollView *)scrollView {
    [self requestData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
